//
//  AddAssetViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/12.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AddAssetViewController.h"
#import "ResizeView.h"
#import "HttpCustomerAction.h"

@interface AddAssetViewController (){
    AddAssetBlock addAssetBlock;
}
@property (nonatomic,strong) CustomerInfo *customerInfo;
@property (nonatomic,strong) UITextField *nameTF;
@property (nonatomic,strong) UITextField *amountTF;
@property (nonatomic,strong) UITextField *yearTF;
@property (nonatomic,strong) UITextField *timeTF;

@end

@implementation AddAssetViewController

-(id)initWithCustomerInfo:(CustomerInfo*)customerInfo block:(AddAssetBlock)block{
    if (self == [super init]) {
        self.customerInfo = customerInfo;
        addAssetBlock = block;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"添加资产";
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:20]];

    ResizeViewInfo *nameRV = [self getNormalResizeViewInfo:@"资产名称:"];
    self.nameTF = (UITextField*)[nameRV viewWithKey:@"TF"];
    _nameTF.placeholder = @"请填写资产名";
    [resizeView addResizeSubview:nameRV];
    
    ResizeViewInfo *amountRV = [self getNormalResizeViewInfo:@"金额:"];
    self.amountTF = (UITextField*)[amountRV viewWithKey:@"TF"];
    _amountTF.keyboardType = UIKeyboardTypeDecimalPad;
    _amountTF.placeholder = @"单位(元)";

    [resizeView addResizeSubview:amountRV];
    
    ResizeViewInfo *yearRV = [self getNormalResizeViewInfo:@"年利率:"];
    self.yearTF = (UITextField*)[yearRV viewWithKey:@"TF"];
    _yearTF.placeholder = @"单位(%)";
    [resizeView addResizeSubview:yearRV];
    
    ResizeViewInfo *timeRV = [self getNormalResizeViewInfo:@"产品周期:"];
    self.timeTF = (UITextField*)[timeRV viewWithKey:@"TF"];
    _timeTF.placeholder = @"单位(月)";
    [resizeView addResizeSubview:timeRV];
    
    [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];
//    [resizeView addResizeSubview:[self ]]
}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 60;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 20).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(void)submitAction:(id)sender{
    if ([self.nameTF.text isEqualToString:@""]) {
        [self showMessage:@"资产名称不能为空" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.amountTF.text isEqualToString:@""]) {
        [self showMessage:@"金额不能为空" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.yearTF.text isEqualToString:@""]) {
        [self showMessage:@"年利率不能为空" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.timeTF.text isEqualToString:@""]) {
        [self showMessage:@"时间不能为空" autoDissmissAfterDelay:0];
        return;
    }
    AssetActionInfo *assetActionInfo = [[AssetActionInfo alloc] init];
    assetActionInfo.userId = self.customerInfo.userId;
    assetActionInfo.assetName = self.nameTF.text;
    assetActionInfo.amount = [self.amountTF.text floatValue];
    assetActionInfo.interestPerYear = [NSString stringWithFormat:@"%.2f",[self.yearTF.text floatValue]/100.0f];
    assetActionInfo.durationMonth = [self.timeTF.text intValue];
    assetActionInfo.buyTime = [Utils dateToString:[NSDate date]];
    [self showWaitingViewWithMessage:@"提交中..." style:WaitingViewShowStyleFullSelfView];
    [HttpCustomerAction postAddAsset:assetActionInfo block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if (!error) {
            [self showMessage:@"添加资产成功" autoDissmissAfterDelay:0];
            if (addAssetBlock) {
                addAssetBlock();
            }
            [self performBlock:^{
                [self.navigationController popViewControllerAnimated:YES];
            } afterDelay:0.8];
        }
    }];
    
}

-(ResizeViewInfo*)getSpaceResizeViewInfo:(int)height{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = height;
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getNormalResizeViewInfo:(NSString*)title{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UITextField *nameTF = [UITextField new];
    nameTF.textColor = [UIColor grayColor];
    nameTF.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:nameTF viewKey:@"TF"];
    nameTF.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 10);
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = DefaultCellLineColor;
    [resizeViewInfo addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(resizeViewInfo.view, 10).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-10).heightIs(1);
    return resizeViewInfo;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
